package net.neiquanlib.base;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.neiquanlib.R;
import net.neiquanlib.widget.RefreshLayout;
import net.neiquanlib.xrecyclerview.ProgressStyle;
import net.neiquanlib.xrecyclerview.XRecyclerView;

import butterknife.ButterKnife;

/**
 * 作者: hjb
 * 时间：2016/3/4.
 */
public abstract class BaseRecycleViewFragment extends BaseFragment implements RefreshLayout.RetryListener, XRecyclerView.LoadingListener {

    public int pageNum = 0;
    public final int PAEG_SIZE = 10;
    public View view;
    public MyBaseAdapterRecycleView adapter;
    public XRecyclerView mRecyclerView;
    public RelativeLayout mHeadView;
    public RefreshLayout mRefreshLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View initRootView() {
        rootView = mInflater.inflate(R.layout.base_recycleview_fragment, null);
        ButterKnife.inject(this, rootView);
        mHeadView = (RelativeLayout) rootView.findViewById(R.id.head_view);
        mRecyclerView = (XRecyclerView) rootView.findViewById(R.id.recyclerView);
        mRefreshLayout = (RefreshLayout) rootView.findViewById(R.id.refreshLayout);
        return rootView;
    }

    private void setHeadView(boolean isHaveHead) {
        if (!isHaveHead) {
            mHeadView.setVisibility(View.GONE);
        } else {
            mHeadView.setVisibility(View.VISIBLE);
        }
    }


    protected abstract MyBaseAdapterRecycleView getAdapter();

    protected abstract int getLineNum();
    protected abstract boolean isHaveHead();

    /**
     * 当返回为true时候设置
     */
    protected abstract void setHeadViews();
    protected abstract void getDateFromWeb(boolean isRefush);


    protected void setTitleTv(int id) {
        TextView tv = (TextView) view.findViewById(R.id.title_tv);
        tv.setText(id);
    }

    protected void setTitleTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.title_tv);
        tv.setText(str);
    }

    protected void setBackTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.back_tv);
        tv.setText(str);
    }

    protected void setNextTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = view.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    protected TextView getNextTv() {

        return (TextView) view.findViewById(R.id.next_tv);
    }

    protected void setBackGone() {
        View tv = view.findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }

    protected void setBackOnClick(View.OnClickListener onClick) {
        LinearLayout ly_back = (LinearLayout) view.findViewById(R.id.ly_back);
        ly_back.setOnClickListener(onClick);
        View iv = view.findViewById(R.id.back_img);
        iv.setOnClickListener(onClick);
    }


    @SuppressLint("NewApi")
    protected void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    protected void setNextImage(int id) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) view.findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    protected void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = view.findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    protected void setNextGone() {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) view.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }


    @Override
    public void setViews() {
        adapter = getAdapter();
        mRecyclerView.setAdapter(adapter);
        setHeadView(isHaveHead());
        setHeadViews();
        int line = 1;
        if (getLineNum() != 0) {
            line = getLineNum();
        }
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(line,
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
//        if (getLineNum() == 1) {
//            mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
//        } else {
//            mRecyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
//        }

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setLoadingListener(this);
        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(true);
//        mRefreshLayout.setRetryListener(this);
        getDateFromWeb(true);
    }

    @Override
    public void initData() {

    }


}
